# import requests
# import json
#
# beaver = 'Bearer bce-v3/ALTAK-WT80OnqVYqiuiXVWpaWYc/448dafad3a1809cdd625d6aeb7b337e73556999d'
# app_id = "5d674ca3-879a-4687-b3de-a6f460a7e57b"
#
# # 步骤一 获取conversation_id
# url = "https://qianfan.baidubce.com/v2/app/conversation"
#
# payload = json.dumps({
#     "app_id": app_id
# })
# headers = {
#     'Content-Type': 'application/json',
#     'X-Appbuilder-Authorization': beaver
# }
#
# response = requests.request("POST", url, headers=headers, data=payload)
#
# print(response.text)
# conversation_id = json.loads(response.text)["conversation_id"]
# print("输出的会话id为：" + conversation_id)
#
#
#
# # 步骤二（可选,若对话时上传文件使用） 获取file_id
# url = "https://qianfan.baidubce.com/v2/app/conversation/file/upload"
#
# payload = {
#     'app_id': app_id,
#     "conversation_id": conversation_id,
#
# }
# files = [
#     ('file', ('456.png', open(r'img/456.png', 'rb'), 'image/jpeg'))
# ]
# headers = {
#     'X-Appbuilder-Authorization': beaver
# }
#
# response = requests.request("POST", url, headers=headers, data=payload, files=files)
#
# print(response.text)
#
# file_id = json.loads(response.text)["id"]
# print("输出的图片id为：" + file_id)
#
# # 步骤三：把问题和参考图片一起输入Agent（自己在AppBuilder中创建并发布的）
# url = "https://qianfan.baidubce.com/v2/app/conversation/runs"
#
# payload = json.dumps({
#     "app_id": app_id,
#     # 用户输入
#     "query": input("请输入问题:"),
#     "stream": False,
#     # 前一个新建会话接口的返回结果
#     "conversation_id": conversation_id,
#     "file_ids": [
#         file_id
#     ]
# })
# headers = {
#     'Content-Type': 'application/json',
#     'X-Appbuilder-Authorization': beaver
# }
#
# response = requests.request("POST", url, headers=headers, data=payload)
#
# print(response.text)
# answer = json.loads(response.text)["answer"]
import requests
import json

beaver = 'Bearer bce-v3/ALTAK-WT80OnqVYqiuiXVWpaWYc/448dafad3a1809cdd625d6aeb7b337e73556999d'
app_id = "5d674ca3-879a-4687-b3de-a6f460a7e57b"

# 步骤一 获取conversation_id
url = "https://qianfan.baidubce.com/v2/app/conversation"

payload = json.dumps({
    "app_id": app_id
})
headers = {
    'Content-Type': 'application/json',
    'X-Appbuilder-Authorization': beaver
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)
conversation_id = json.loads(response.text)["conversation_id"]
print("输出的会话id为：" + conversation_id)



# 步骤二（可选,若对话时上传文件使用） 获取file_id
url = "https://qianfan.baidubce.com/v2/app/conversation/file/upload"

payload = {
    'app_id': app_id,
    "conversation_id": conversation_id,

}
files = [
    ('file', ('456.png', open(r'img/456.png', 'rb'), 'image/jpeg'))
]
headers = {
    'X-Appbuilder-Authorization': beaver
}

response = requests.request("POST", url, headers=headers, data=payload, files=files)

print(response.text)

file_id = json.loads(response.text)["id"]
print("输出的图片id为：" + file_id)

# 步骤三：把问题和参考图片一起输入Agent（自己在AppBuilder中创建并发布的）
url = "https://qianfan.baidubce.com/v2/app/conversation/runs"

payload = json.dumps({
    "app_id": app_id,
    # 用户输入
    "query": input("请输入问题:"),
    "stream": False,
    # 前一个新建会话接口的返回结果
    "conversation_id": conversation_id,
    "file_ids": [
        file_id
    ]
})
headers = {
    'Content-Type': 'application/json',
    'X-Appbuilder-Authorization': beaver
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)
answer = json.loads(response.text)["answer"]